/**  
 * Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
 * in source and binary forms, with or without modification, are permitted  
 * provided that the following conditions are met: Redistributions of source  
 * code must retain the above copyright notice, this list of conditions and the  
 * following disclaimer. Redistributions in binary form must reproduce the above  
 * copyright notice, this list of conditions and the following disclaimer in the  
 * documentation and/or other materials provided with the distribution. Neither  
 * the name of Noel Lynch nor the names of contributors may be used to  
 * endorse or promote products derived from this software without specific prior  
 * written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 */
// Jul 30, 2008

package com.sceneworker.monitorpopup;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.util.concurrent.Callable;

import javax.swing.JDialog;
import javax.swing.tree.TreePath;

import com.jme.image.Texture;
import com.jme.scene.Geometry;
import com.jme.scene.Spatial;
import com.jme.scene.state.TextureState;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import com.myjavautils.GenericUtils;
import com.sceneworker.app.globals.SceneWorkerDialogManager;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.monitorpopup.panels.NewJava2DRenderPanel;
import com.sceneworker.monitorpopup.panels.NewTextureRendererPanel;

public class TextureStateMenu extends BaseMenu implements ActionListener {

	private static final String NEW_TEXTURE_JAVA_2D = "New Texture Using Java2D";
	private static final String PASTE_TEXTURE_REFERENCE = "Paste Texture Reference";
	private static final String NEW_RENDER_TO_TEXTURE = "New Render To Texture";

	private static final String LOAD_TEXTURE = "Load Texture";

	private		TextureState		mcl_ts;
	private		Object				mcl_spatial;

	public	TextureStateMenu(TreePath cl_selectedPath, SceneWorkerPopUp cl_menu) {
		super(cl_menu);

		mcl_ts = (TextureState)cl_selectedPath.getLastPathComponent();
		mcl_spatial = cl_selectedPath.getParentPath().getLastPathComponent();

		buildMenu();
	}

	@Override
	protected	void
	buildMenu() {
		mcl_menu.addSeparator();
		mcl_menu.add(buildAMenuItem(LOAD_TEXTURE));
		mcl_menu.add(buildAMenuItem(NEW_RENDER_TO_TEXTURE));
		mcl_menu.add(buildAMenuItem(NEW_TEXTURE_JAVA_2D));
		
		if(mcl_menu.getObjectBuffer() instanceof Texture) {
			mcl_menu.add(buildAMenuItem(PASTE_TEXTURE_REFERENCE));
		}
	}

	public void actionPerformed(ActionEvent cl_e) {
		if(cl_e.getActionCommand().equals(LOAD_TEXTURE)) {
			//final	File	lcl_f = SceneWorkerPreferences.inst().getAFile(null, false, false);
			final	File	lcl_f = GenericUtils.getAnImageFile(null);
			
			if(lcl_f == null) { return; }

			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					try {
						Texture		lcl_texture = TextureManager.loadTexture(lcl_f.toURL(), Texture.MinificationFilter.Trilinear, 
								Texture.MagnificationFilter.Bilinear);
						
						addTexture(lcl_texture);
						
						} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return null;
				}
				
			});
		}
		
		if(cl_e.getActionCommand().equals(NEW_RENDER_TO_TEXTURE)) {
			JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.NEW_TEXTURE_RENDERER);
			lcl_dlg.add(new NewTextureRendererPanel(mcl_ts, mcl_spatial, lcl_dlg));
			lcl_dlg.setVisible(true);
		}
		
		if(cl_e.getActionCommand().equals(NEW_TEXTURE_JAVA_2D)) {
			JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.NEW_JAVA_2D_TEXTURE);
			lcl_dlg.add(new NewJava2DRenderPanel(mcl_ts, (Spatial)mcl_spatial, lcl_dlg));
			lcl_dlg.setVisible(true);
		}
		
		if(cl_e.getActionCommand().equals(PASTE_TEXTURE_REFERENCE)) {
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					Texture		lcl_texture = (Texture)mcl_menu.getObjectBuffer();
					addTexture(lcl_texture);
					return null;
				}
			});
		}
	}

	private	void	addTexture(Texture cl_texture) {
		mcl_ts.setTexture(cl_texture, mcl_ts.getNumberOfSetTextures());

		if(mcl_spatial instanceof Geometry) {
			((Geometry)mcl_spatial).copyTextureCoordinates(0, mcl_ts.getNumberOfSetTextures() - 1, 1);
		}

		mcl_ts.setNeedsRefresh(true);
		if(mcl_spatial instanceof Spatial) {
			((Spatial)mcl_spatial).updateRenderState();
			((Spatial)mcl_spatial).updateGeometricState(0, true);
		}
	}
}
